﻿using System;
using System.Collections.Generic;
using System.Text;
using AspNetCs.IDAL;
using System.Data;
using AspNetCs.Model;
using AspNetCs.Common;

namespace AspNetCs.SQLServerDAL
{
    public class Temple_DAL : ITemple
    {

        #region ITemple 成员

        public int Add(AspNetCs.Model.Temple_Model model)
        {
            int result = 0;
            db.ParameterArry[] par = new db.ParameterArry[6];
            par[0].dbType = SqlDbType.VarChar;
            par[0].objValue = model.TempleName;
            par[0].parName = "@TempleName";

            par[1].dbType = SqlDbType.VarChar;
            par[1].objValue = model.TemplePath;
            par[1].parName = "@TemplePath";

            par[2].dbType = SqlDbType.VarChar;
            par[2].objValue = model.Thumbimg;
            par[2].parName = "@Thumbimg";

            par[3].dbType = SqlDbType.Decimal;
            par[3].objValue = model.Point;
            par[3].parName = "@Point";

            par[4].dbType = SqlDbType.Int;
            par[4].objValue = model.ChannelID;
            par[4].parName = "@ChannelID";

            par[5].dbType = SqlDbType.Int;
            par[5].objValue = model.Types;
            par[5].parName = "@Types";
            db Conn = new db();

            string sqlstr = "Insert into TempleList(TempleName,TemplePath,Thumbimg,Point,ChannelID,Types)";
            sqlstr += " values(@TempleName,@TemplePath,@Thumbimg,@Point,@ChannelID,@Types)";

            try
            {
                result = Conn.ExcSql(sqlstr, par);
            }
            catch
            {
                result = -1;
            }
            finally
            {
                Conn.close();
            }
            return result;

        }

        public int delete(string filter)
        {
            int result = 0;
            db Conn = new db();
            try
            {
                result = Conn.ExcSql("delete From TempleList where " + filter);
            }
            catch
            {
                result = -1;
            }
            finally
            {
                Conn.close();
            }
            return result;
        }

        public AspNetCs.Model.Temple_Model GetModel(System.Data.DataRow dr)
        {
            Temple_Model model = new Temple_Model();
            if (dr["id"] != null) model.Id = StringHelper.isNumber_int(dr["id"].ToString());
            if (dr["TempleName"] != null) model.TempleName = dr["TempleName"].ToString();
            if (dr["TemplePath"] != null) model.TemplePath = dr["TemplePath"].ToString();
            if (dr["Thumbimg"] != null) model.Thumbimg = dr["Thumbimg"].ToString();
            if (dr["Point"] != null) model.Point = StringHelper.isNumber_decimal(dr["Point"].ToString());
            if (dr["Addtime"] != null) model.Addtime = StringHelper.isDatetime(dr["Addtime"].ToString());
            if (dr["ChannelID"] != null) model.ChannelID = StringHelper.isNumber_int(dr["ChannelID"].ToString());
            if (dr["Types"] != null) model.Types = StringHelper.isNumber_int(dr["Types"].ToString());
            return model;
        }

        public DataTable GetDataTable(string filter)
        {
            db Conn = new db();
            if (filter != "")
            {
                filter = " where " + filter;
            }
            return Conn.GetSql_DataTable("Select * from TempleList " + filter);
        }

        public int Update(Temple_Model model)
        {
            int result = 0;
            db.ParameterArry[] par = new db.ParameterArry[6];
            par[0].dbType = SqlDbType.VarChar;
            par[0].objValue = model.TempleName;
            par[0].parName = "@TempleName";

            par[1].dbType = SqlDbType.VarChar;
            par[1].objValue = model.TemplePath;
            par[1].parName = "@TemplePath";

            par[2].dbType = SqlDbType.VarChar;
            par[2].objValue = model.Thumbimg;
            par[2].parName = "@Thumbimg";

            par[3].dbType = SqlDbType.Decimal;
            par[3].objValue = model.Point;
            par[3].parName = "@Point";

            par[4].dbType = SqlDbType.Int;
            par[4].objValue = model.Id;
            par[4].parName = "@Id";

            par[5].dbType = SqlDbType.Int;
            par[5].objValue = model.Types;
            par[5].parName = "@Types";
            db Conn = new db();
            try
            {
                string Sqlstr = "Update TempleList Set TempleName=@TempleName,TemplePath=@TemplePath,Thumbimg=@Thumbimg,Point=@Point,Types=@Types  where id=@id";

                result = Conn.ExcSql(Sqlstr, par);
            }
            catch
            {
                result = -1;
            }
            finally
            {
                Conn.close();
            }
            return result;

        }

        public DataTable GetTableColumn(string TbName)
        {
            DataTable dt = new DataTable();
            try
            {

                dt.Columns.Add(new DataColumn("id", Type.GetType("System.Int32")));
                dt.Columns.Add(new DataColumn("ColumnName", Type.GetType("System.String")));

                db ConnTb = new db();
                DataTable dt1 = ConnTb.GetSql_DataTable("Select * from " + TbName + "");
                ConnTb.close();
                int i = 0;

                foreach (DataColumn dc in dt1.Columns)
                {
                    i++;
                    DataRow dr = dt.NewRow();
                    dr["id"] = i;
                    dr["ColumnName"] = dc.ColumnName.ToString();
                    dt.Rows.Add(dr);
                }
            }
            catch
            {
                dt = null;
            }
            return dt;

        }

        public DataTable GetTableColumn(string TbName, string filter)
        {
            DataTable dt = new DataTable();

            try
            {
                if (filter != "")
                {
                    filter = "  where " + filter;
                }
                string sqlstr = "Select * from " + TbName + filter;

                db ConnTb = new db();
                dt = ConnTb.GetSql_DataTable(sqlstr);
            }
            catch
            {
                dt = null;
            }

            return dt;
        }

        #endregion
    }
}
